﻿CREATE PROCEDURE [acms].[CustomAdminPage_Put]
	@Id int, 
	@SiteId int,
	@Name varchar(50),
	@ControlPath varchar(255),
	@Notes varchar(2000),
	@Sequence int,
	@Section tinyint,
	@ConcurencyId rowversion,
	@IdOut int output,
	@SequenceOut int output,
	@ConcurencyIdOut rowversion output
AS
	if @Id is null or @Id = 0 begin
		select @Sequence = isnull(max(sequence) + 1, 0) from [CustomAdminPage_View] where SiteId = @SiteId
		
		insert into [CustomAdminPage]
			(SiteId, Name, ControlPath, Notes, Sequence, Section)
		values
			(@SiteId, @Name, @ControlPath, @Notes, @Sequence, @Section)
		set @IdOut = @@Identity
	end
	else if Exists (Select null from [CustomAdminPage] where Id = @Id and ConcurencyId = @ConcurencyId and Deleted = 0) begin
		update [CustomAdminPage]
		set 
			Name = @Name,
			ControlPath = @ControlPath, 
			Notes = @Notes,
			Sequence = @Sequence,
			Section = @Section
		where Id = @Id and ConcurencyId = @ConcurencyId and Deleted = 0
		set @IdOut = @Id
	end
	else if Exists (Select null from [CustomAdminPage] where Id = @Id and Deleted = 0) begin
		return -100
	end
	else begin
		return -200
	end	
		
	select @SequenceOut = Sequence, @ConcurencyIdOut = ConcurencyId from [CustomAdminPage_View] where Id = @IdOut
RETURN 0;